package com.feng.cloud.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.cloud.common.constants.SystemConstant;
import com.feng.cloud.common.exception.SystemException;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.service.RedisService;
import com.feng.cloud.common.utils.SortUtils;
import com.feng.cloud.system.entity.OAuthClientDetails;
import com.feng.cloud.system.mapper.OAuthClientDetailsMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * @ClassName OAuthClientDetailsService
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/30 10:28
 * @Version 1.0
 **/
@Service
@Slf4j
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
@AllArgsConstructor
public class OAuthClientDetailsService extends ServiceImpl<OAuthClientDetailsMapper, OAuthClientDetails> {

    private final PasswordEncoder passwordEncoder;
    private final RedisService redisService;

    /**
     * 分页查询
     * @param request request
     * @return IPage<OAuthClientDetails>
     */
    public IPage<OAuthClientDetails> selectListByPage(QueryRequest request) {
        LambdaQueryWrapper<OAuthClientDetails> queryWrapper = new LambdaQueryWrapper<>();
        String clientId = request.getValue(OAuthClientDetails::getClientId,String.class);
        if (StringUtils.isNotBlank(clientId)) {
            queryWrapper.like(OAuthClientDetails::getClientId, clientId);
        }
        Page<OAuthClientDetails> page = new Page<>();
        SortUtils.handlePageAndSort(request,page,OAuthClientDetails::getCreateTime, SystemConstant.ORDER_DESC);
        return this.baseMapper.selectPage(page, queryWrapper);
    }

    public void addClient(OAuthClientDetails clientDetails) {
        // 保存加密前的密码
        clientDetails.setOriginSecret(clientDetails.getClientSecret());
        clientDetails.setClientSecret(passwordEncoder.encode(clientDetails.getClientSecret()));
        clientDetails.setCreateTime(LocalDateTime.now());
        this.save(clientDetails);
    }

    public void updateClient(OAuthClientDetails clientDetails) {
        String clientId = clientDetails.getClientId();
        clientDetails.setClientSecret(null);
        clientDetails.setModifyTime(LocalDateTime.now());
        if (StringUtils.isBlank(clientId)) {
            throw new SystemException(SystemStatus.CLIENT_ID_EMPTY);
        }
        // 清除缓存
        redisService.hdel(SystemConstant.CLIENT_DETAILS_PREFIX, clientId);
        // 修改数据库
        this.baseMapper.updateById(clientDetails);
    }

    public void deleteClients(String[] ids) {
        // 清除缓存
        redisService.hdel(SystemConstant.CLIENT_DETAILS_PREFIX, ids);
        // 删除数据库
        this.baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    public OAuthClientDetails selectByClientId(String clientId) {
        if (StringUtils.isBlank(clientId)) {
            throw new SystemException(SystemStatus.CLIENT_ID_EMPTY);
        }
        return this.baseMapper.selectById(clientId);
    }
}
